var app = getApp()
//常量数据配置
var ConstantData = require('../../../../../../common/constant_data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    noRecord:0,
    time:'',
    date:'',
    backSelTimeStat:false,
    backTypeArr: ['租金', '月租', '赔偿'],
    backTypeObjArr: [{ val: "0" }, { val: "3" }, { val: "1" }],
    backIndex: 0,
    leasePrice:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var curDate = new Date();
    var year = curDate.getFullYear();
    var month = curDate.getMonth() + 1;
    var day = curDate.getDate();
    var hour = curDate.getHours(); 
    var minute = curDate.getMinutes();
    this.setData({
       date:year + "-" + (month <10 ? '0'+month : month) + "-" + (day < 10 ? '0'+day : day),
       time:(hour < 10 ? '0' + hour : hour )+":"+(minute < 10 ? '0' + minute : minute)+":00" 
    }); 

    var eqId = options.eqId;
    var phone = options.phone;
    console.log("--->>>",eqId,phone);
    var that = this;
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/lease/lease_return/list',
        header: header,
        data: {
          eqId:eqId,
          phone:phone
        },
        success: function (res) {
          var recordList = res.data;
          console.log("--->",res);
          var noRecord = 0;
          if(recordList.length == 0) {
            noRecord = 1;
          }
          for(var i=0;i<recordList.length;i++) {
            recordList[i].uur_open_lock_time = getShowDate(recordList[i].uur_open_lock_time);
            recordList[i].record_return_time = getShowDate(recordList[i].record_return_time);
          }
          that.setData({
            leseList:recordList,
            noRecord:noRecord
          });
        }
      })
    });  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 归还用户轮椅
   */
  backChair:function(e) {
    var orderCode = e.currentTarget.dataset.ordercode;
    var backtime = e.currentTarget.dataset.backtime;
    var opentime = e.currentTarget.dataset.opentime;
    var backDate = this.data.date;
    var backTime = this.data.time;
    if(backtime) {
      var arr = backtime.split(" ");
      backDate = arr[0];
      backTime = arr[1];
    }else {
      var arr = opentime.split(" ");
      backDate = arr[0];
      backTime = arr[1];
    }
    this.setData({
        backSelTimeStat:true,
        orderCode:orderCode,
        date:backDate,
        time:backTime
    })
  },
  /**
   * 执行归还操作
   */
  excBackChair:function() {
    var that = this;
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/lease/backchair',
        header: header,
        data: {
          orderCode: that.data.orderCode,
          returnTime: that.data.date + " " + that.data.time,
          price: that.data.leasePrice,
          calType:that.data.backTypeObjArr[that.data.backIndex].val
        },
        success: function (res) {
          console.log("backchair",res);
          if (res.data.result === 0) {
            wx.showToast({
              title: '归还成功',
              icon:'none'
            });
            that.setData({
              backSelTimeStat:false,
              orderCode:''
            })
          }else {
            wx.showToast({
              title: '归还失败',
              icon: 'none'
            });
          }
        },
        fail:function(res) {
          wx.showToast({
            title: '调用失败',
            icon:"none"
          })
        }
      })
    });  
  },
  /**
   * 变更归还日期
   */
  bindDateChange:function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  /**
   * 变更归还时间
   */
  bindTimeChange:function(e) {
    this.setData({
      time: e.detail.value + ":00"
    })
  },
  /**
   * 取消归还
   */
  cancelBack:function() {
     this.setData({
       backSelTimeStat:false,
       orderCode:''
     })
  },
  /**
   * 选择归还类型
   */
  bindBackTypePickerChange:function(e) {
      this.setData({
        backIndex: e.detail.value
      }) 
  },
  /**
   * 设置租赁的费用
   */
  setLeasePrice:function(e) {
      this.setData({
        leasePrice: e.detail.value
      })
  }
})

function getShowDate(timestamp) {
  if(!timestamp) {
    return '';
  }
  var date = new Date(timestamp); //获取一个时间对象
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate() + ' ';
  var hour = date.getHours();
  var h = (hour < 10 ? '0' + hour : hour) + ':';
  var minute = date.getMinutes();
  var m = (minute < 10 ? '0' + minute : minute) + ':';
  var second = date.getSeconds();
  var s = second <10 ? '0' + second : second;
  return Y + M + D + h + m + s; 
}